package com.sduept.nwld.dataserver.controller.statistics;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.statistics.TransmissionEquipmentManager;
import com.sduept.nwld.dataserver.model.countanalyse.BusbarInfo;
import com.sduept.nwld.dataserver.model.countanalyse.TansformerInfo;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Named
@ViewScoped
public class TransmissionEquipmentController extends AbstractController {

	private static final long serialVersionUID = -1523229436540714217L;

	@Autowired
	private TransmissionEquipmentManager manager;

	private List<TansformerInfo> tansformerInfos = new ArrayList<TansformerInfo>();
	private TansformerInfo tansformerInfo = new TansformerInfo();
	private TansformerInfo selectTansformerInfo = new TansformerInfo();

	private List<BusbarInfo> busbarInfos = new ArrayList<BusbarInfo>();
	private BusbarInfo busbarInfo = new BusbarInfo();
	private BusbarInfo selectBusbarInfo = new BusbarInfo();

	private String year;
	private String voltage;

	@PostConstruct
	private void init() {
		tansformerInfos = manager.getAllTansformerInfo();
		busbarInfos = manager.getAllBusbarInfo();
	}

	public void preCreatTansformerInfo() {
		tansformerInfo = new TansformerInfo();
		year = null;
		voltage = null;
	}

	public void preCreatBusbarInfo() {
		busbarInfo = new BusbarInfo();
		year = null;
		voltage = null;
	}

	public void editTansformerInfo() {
		tansformerInfo = selectTansformerInfo;
		year = selectTansformerInfo.getYear().toString();
		voltage = selectTansformerInfo.getVoltage() == null ? "" : selectTansformerInfo.getVoltage().toString();
	}

	public void editBusbarInfo() {
		busbarInfo = selectBusbarInfo;
		year = selectBusbarInfo.getYear().toString();
		voltage = selectBusbarInfo.getVoltage() == null ? "" : selectBusbarInfo.getVoltage().toString();
	}

	public void saveTansformerInfo() {
		if (tansformerInfo != null) {
			if (null != year) {
				tansformerInfo.setYear(Integer.parseInt(year));
			}
			if (null != voltage && !"0".equals(voltage)) {
				tansformerInfo.setVoltage(Integer.parseInt(voltage));
			}
			if (tansformerInfo.getId() == null) {
				// 新建
				if (!isExist(tansformerInfo)) {
					countNewAddTansformerInfo(tansformerInfo);
				} else {
					addErrorMessage("该年份数据已存在！");
					return;
				}
			} else {
				// 编辑
				countNewAddTansformerInfo(tansformerInfo);
				selectTansformerInfo = new TansformerInfo();
			}
			addSuccessMessage("保存成功");
		}
		tansformerInfos = manager.getAllTansformerInfo();
	}

	/**
	 * 计算当前年份的台数与上一年的新增
	 * @param current
	 */
	private void countNewAddTansformerInfo(TansformerInfo current) {
		TansformerInfo[] obj = getPreAndNextInfo(current);
		TansformerInfo pre = obj[0];
		TansformerInfo next = obj[1];
		getNewAddBetweenTwoRecords(pre, current);
		if (null != pre) {
			manager.createOrUpdateTansformerInfo(pre);
		}
		getNewAddBetweenTwoRecords(current, next);
		if (null != next) {
			manager.createOrUpdateTansformerInfo(next);
		}
		manager.createOrUpdateTansformerInfo(current);
	}

	/**
	 * 计算两条记录之间的变压器新增台数
	 * @param pre
	 * @param current
	 */
	private void getNewAddBetweenTwoRecords(TansformerInfo pre, TansformerInfo current) {
		if (null == pre) {
			current.setNewAdd(0);
		}
		if (null != pre && null != current) {
			int newAdd = current.getCount() - pre.getCount();
			current.setNewAdd(newAdd);
		}
	}

	/**
	 * 为计算新增数量，获取当前记录的前一条和后一条记录
	 * @param current
	 * @return
	 */
	private TansformerInfo[] getPreAndNextInfo(TansformerInfo current) {
		Integer voltage = current.getVoltage();
		List<TansformerInfo> list = manager.getTansformerInfos(null, voltage);
		TansformerInfo pre = null;
		TansformerInfo next = null;
		if (!list.contains(current)) {
			list.add(current);
		}
		if (list.size() > 1) {
			sortListByYear(list);
			int index = list.indexOf(current); // 当前记录在list中的位置
			// 获取前一条数据
			if (index > 0) {
				pre = list.get(index - 1);
			}
			// 获取后一条数据
			if (index < list.size() - 1) {
				next = list.get(index + 1);
			}
		}
		TansformerInfo[] obj = { pre, next };
		return obj;
	}

	private void sortListByYear(List<TansformerInfo> list) {
		// 重新排序
		Collections.sort(list, new Comparator<TansformerInfo>() {
			@Override
			public int compare(TansformerInfo o1, TansformerInfo o2) {
				return o1.getYear() - o2.getYear();
			}
		});
	}

	private boolean isExist(TansformerInfo info) {
		List<TansformerInfo> lt = manager.getTansformerInfos(info.getYear(), info.getVoltage());
		if (lt.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public void saveBusbarInfo() {
		if (busbarInfo != null) {
			if (null != year) {
				busbarInfo.setYear(Integer.parseInt(year));
			}
			if (null != voltage && !"0".equals(voltage)) {
				busbarInfo.setVoltage(Integer.parseInt(voltage));
			}
			if (busbarInfo.getId() == null) {// 新建
				if (isExist(busbarInfo)) {
					addErrorMessage("该年份数据已存在！");
					return;
				} else {
					countNewAddBusbarInfo(busbarInfo);
				}
			} else { // 编辑
				countNewAddBusbarInfo(busbarInfo);
				selectBusbarInfo = new BusbarInfo();
			}
			addSuccessMessage("保存成功");
		}
		busbarInfos = manager.getAllBusbarInfo();
	}

	/**
	 * 计算当前年份的母线条数比上一年的新增
	 * @param current
	 */
	private void countNewAddBusbarInfo(BusbarInfo current) {
		BusbarInfo[] obj = getPreAndNextInfo(current);
		BusbarInfo pre = obj[0];
		BusbarInfo next = obj[1];
		getNewAddBetweenTwoRecords(pre, current);
		if (null != pre) {
			manager.createOrUpdateBusbarInfo(pre);
		}
		getNewAddBetweenTwoRecords(current, next);
		if (null != next) {
			manager.createOrUpdateBusbarInfo(next);
		}
		manager.createOrUpdateBusbarInfo(current);
	}

	/**
	 * 计算两条记录之间的母线新增条数
	 * @param pre
	 * @param current
	 */
	private void getNewAddBetweenTwoRecords(BusbarInfo pre, BusbarInfo current) {
		if (null == pre) {
			current.setNewAdd(0);
		}
		if (null != pre && null != current) {
			int newAdd = current.getCount() - pre.getCount();
			current.setNewAdd(newAdd);
		}
	}

	/**
	 * 为计算母线新增条数，获取当前记录的前一条和后一条记录
	 * @param current
	 * @return
	 */
	private BusbarInfo[] getPreAndNextInfo(BusbarInfo current) {
		Integer voltage = current.getVoltage();
		List<BusbarInfo> list = manager.getBusbarInfos(null, voltage);
		BusbarInfo pre = null;
		BusbarInfo next = null;
		if (!list.contains(current)) {
			list.add(current);
		}
		if (list.size() > 1) {
			sortBusbarInfosByYear(list);
			int index = list.indexOf(current); // 当前记录在list中的位置
			// 获取前一条数据
			if (index > 0) {
				pre = list.get(index - 1);
			}
			// 获取后一条数据
			if (index < list.size() - 1) {
				next = list.get(index + 1);
			}
		}
		BusbarInfo[] obj = { pre, next };
		return obj;
	}

	private void sortBusbarInfosByYear(List<BusbarInfo> list) {
		// 重新排序
		Collections.sort(list, new Comparator<BusbarInfo>() {
			@Override
			public int compare(BusbarInfo o1, BusbarInfo o2) {
				return o1.getYear() - o2.getYear();
			}
		});
	}

	private boolean isExist(BusbarInfo info) {
		List<BusbarInfo> lt = manager.getBusbarInfos(info.getYear(), info.getVoltage());
		if (lt.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public void deleteTansformerInfo() {
		if (selectTansformerInfo != null && !selectTansformerInfo.getId().isEmpty()) {
			updateNextWhenDelete(selectTansformerInfo);
			manager.deleteTansformerInfo(selectTansformerInfo.getId());
			selectTansformerInfo = new TansformerInfo();
			addSuccessMessage("删除成功！");
		} else {
			addErrorMessage("请首先选择记录！");
		}
		tansformerInfos = manager.getAllTansformerInfo();
	}

	private void updateNextWhenDelete(TansformerInfo current) {
		TansformerInfo[] obj = getPreAndNextInfo(current);
		TansformerInfo pre = obj[0];
		TansformerInfo next = obj[1];
		if (null != next) {
			getNewAddBetweenTwoRecords(pre, next);
			manager.createOrUpdateTansformerInfo(next);
		}
	}

	public void deleteBusbarInfo() {
		if (selectBusbarInfo != null && !selectBusbarInfo.getId().isEmpty()) {
			updateNextWhenDelete(selectBusbarInfo);
			manager.deleteBusbarInfo(selectBusbarInfo.getId());
			selectBusbarInfo = new BusbarInfo();
			addSuccessMessage("删除成功！");
		} else {
			addErrorMessage("请首先选择记录！");
		}
		busbarInfos = manager.getAllBusbarInfo();
	}

	private void updateNextWhenDelete(BusbarInfo current) {
		BusbarInfo[] obj = getPreAndNextInfo(current);
		BusbarInfo pre = obj[0];
		BusbarInfo next = obj[1];
		if (null != next) {
			getNewAddBetweenTwoRecords(pre, next);
			manager.createOrUpdateBusbarInfo(next);
		}
	}

	public void onRowSelect(SelectEvent event) {
		selectBusbarInfo = (BusbarInfo) event.getObject();
	}

	public void onTansformerInfoSelect(SelectEvent event) {
		selectTansformerInfo = (TansformerInfo) event.getObject();
	}

	public List<TansformerInfo> getTansformerInfos() {
		return tansformerInfos;
	}

	public void setTansformerInfos(List<TansformerInfo> tansformerInfos) {
		this.tansformerInfos = tansformerInfos;
	}

	public TansformerInfo getTansformerInfo() {
		return tansformerInfo;
	}

	public void setTansformerInfo(TansformerInfo tansformerInfo) {
		this.tansformerInfo = tansformerInfo;
	}

	public TansformerInfo getSelectTansformerInfo() {
		return selectTansformerInfo;
	}

	public void setSelectTansformerInfo(TansformerInfo selectTansformerInfo) {
		this.selectTansformerInfo = selectTansformerInfo;
	}

	public List<BusbarInfo> getBusbarInfos() {
		return busbarInfos;
	}

	public void setBusbarInfos(List<BusbarInfo> busbarInfos) {
		this.busbarInfos = busbarInfos;
	}

	public BusbarInfo getBusbarInfo() {
		return busbarInfo;
	}

	public void setBusbarInfo(BusbarInfo busbarInfo) {
		this.busbarInfo = busbarInfo;
	}

	public BusbarInfo getSelectBusbarInfo() {
		return selectBusbarInfo;
	}

	public void setSelectBusbarInfo(BusbarInfo selectBusbarInfo) {
		this.selectBusbarInfo = selectBusbarInfo;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getVoltage() {
		return voltage;
	}

	public void setVoltage(String voltage) {
		this.voltage = voltage;
	}

}
